package com.zhl.media.app.thread;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import com.birbit.android.jobqueue.Job;
import com.birbit.android.jobqueue.Params;
import com.birbit.android.jobqueue.RetryConstraint;
import com.jess.arms.utils.LogUtils;
import com.orhanobut.hawk.Hawk;
import com.zhl.media.app.BuildConfig;
import com.zhl.media.app.event.Event;
import com.zhl.media.app.model.MediaPhoto;
import com.zhl.media.commonsdk.data.UserUtil;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class UploadJob extends Job {
    private MediaPhoto mediaPhoto;
    public int postion;
    public UploadJob(MediaPhoto mediaPhoto, int postion) {
        super(new Params(1).requireNetwork());
        this.mediaPhoto = mediaPhoto;
        this.postion = postion;
    }
    @Override
    public void onAdded() {
    }
    @Override
    public void onRun() throws Throwable {
        Thread.sleep(200);
        String url = "";
        //1 新增
        if(mediaPhoto.getReload()==1){
             url = BuildConfig.SERVER_URL + "pushResult/saveResult";
        }
        //2修改
        if(mediaPhoto.getReload()==2){
            url =  BuildConfig.SERVER_URL + "pushResult/updateResultPicture";
        }
        File file = new File(mediaPhoto.getPhotoPath());
//        File file = new File(Environment.getExternalStorageDirectory() + "/a.exe");
        String token = UserUtil.getTokenInfo().getAccess_token();
        MultipartBody.Builder builder= new MultipartBody.Builder().setType(MultipartBody.FORM);
        builder.addFormDataPart("file", file.getName(), RequestBody.create(MediaType.parse("multipart/form-data"), file));
        builder.addFormDataPart("mediaId",mediaPhoto.getMediaId());
        builder.addFormDataPart("pushDate",Hawk.get("thisDate"));
        builder.addFormDataPart("pushStatus","1");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        builder.addFormDataPart("photographTime",sdf.format(new Date(mediaPhoto.getPhotoDate())));
        builder.addFormDataPart("longitude",mediaPhoto.getLongitude()+"");
        builder.addFormDataPart("latitude",mediaPhoto.getLatitude()+"");
        Request request = new Request.Builder().header("Authorization", "Bearer" + token).url(url).post(builder.build()).build();
        OkHttpClient client = new OkHttpClient();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Event.postUploadOk(postion);
            }
        });
    }
    @Override
    protected void onCancel(int cancelReason, @Nullable Throwable throwable) {

    }

    @Override
    protected RetryConstraint shouldReRunOnThrowable(@NonNull Throwable throwable, int runCount, int maxRunCount) {
        return RetryConstraint.RETRY;
    }
}

